package byj.com.meishi.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/***
 * 
 * @类名称: HttpUtil
 * @类描述: 网络访问工具类
 * @创建人：一一哥
 * @创建时间：2016年8月19日 上午10:12:16
 * @备注：
 * @version V1.0
 */
public class HttpUtil {

	public static byte[] loadData(String path) throws IOException {
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5000);
		if (conn.getResponseCode() == 200) {
			InputStream is = conn.getInputStream();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			byte[] buffer = new byte[1024];
			while ((len = is.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
				baos.flush();
			}

			byte[] data = baos.toByteArray();
			is.close();
			baos.close();
			return data;
		}
		return null;
	}

}
